// pages/userInfo/index.js
const app = getApp()
var uid;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canse: false,
    canUse: wx.canIUse('button.open-type.getUserInfo'),
    hitenTop:false,
    reminder: true,
    showLoading:false,
    isFolded: true,
    count: 10,
    userMessage: [],
    userCardInfo: '',
    userBasic: {},
    bagImg: [
      '/images/icon/userbgm.png',
      '/images/icon/uscarbgm.png',
      '/images/icon/recom.png',
      '/images/icon/recomup.png',
      '/images/icon/msg.png',
      '/images/icon/write.png',
      '/images/icon/icon3.png',
      '/images/icon/icon5.png',
      '/images/icon/icon7.png',
    ],
    messageModalput: true,
    message: '',
    // 触摸开始时间
    touchStartTime: 0,
    // 触摸结束时间
    touchEndTime: 0,
    // 最后一次单击事件点击发生时间
    lastTapTime: 0,
    // 单击事件点击后要触发的函数
    lastTapTimeoutFunc: null,
  },
  baseImage: function() {
    var that = this;
    var data = that.data.bagImg;
    console.log('转码', data)
    for (var i = 0; i < data.length; i++) {
      var bgm = app.baseImage(data[i]);
      data[i] = bgm;
    }
    that.setData({
      bagImg: data
    })
  },
  change: function (e) {
    this.setData({
      isFolded: !this.data.isFolded,
    })
  },
  getStorage: function () {
    var that = this;
    try {
      const value = wx.getStorageSync('userInfo')
      console.log('读取缓存', value)
      if (value) {
        uid = value.uid;
        that.getUserBasicInfo();
        that.getUserNewMessageReply();
        that.getPersonalUserInfo();
      } else {
        that.getUserInfo();
      }
    } catch (e) {
      //this.getUserInfo();
      app.prompt('登陆失败' + e);
    }
  },
  /**
     * 授权获取用户信息
     */
  getUserInfo: function () {
    var that = this;
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          console.log('已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框')
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          app.getUserInfo(that);
        } else {
          that.setData({
            canse: true
          })
        }
      }
    })
  },
  /**
 * 用户授权
 */
  bindGetUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      this.getUserInfo();
    } else {
      app.prompt('登陆失败');
    }
  },
  //获取个人基本信息
  getUserBasicInfo: function () {
    var that = this;
    var data = [];
    data['uid'] = uid;
    var url = 'We_Community/getBrowsePersonalUserBasic'
    var calkdata = app.netWorkRequest(that, data, url, 'userBasic')
  },
  //查询用户是否存新留言或未读消息
  getUserNewMessageReply: function () {
    var that = this;
    var data = [];
    data['uid'] = uid;
    var url = 'We_Community/getNewMessageReply';
    var calkdata = app.netWorkRequest(that, data, url, 'userMessage')
  },
  /// 按钮触摸开始触发的事件
  touchStart: function(e) {
    this.touchStartTime = e.timeStamp
  },

  /// 按钮触摸结束触发的事件
  touchEnd: function(e) {
    this.touchEndTime = e.timeStamp
  },

  /// 单击、双击
  multipleTap: function(e) {
    console.log('获取参数', e)
    var that = this;
    var t = that.data.userCardInfo;
    var id = e.currentTarget.id;
    // 控制点击事件在350ms内触发，加这层判断是为了防止长按时会触发点击事件
    if (that.touchEndTime - that.touchStartTime < 350) {
      // 当前点击的时间
      var currentTime = e.timeStamp
      var lastTapTime = that.lastTapTime
      // 更新最后一次点击时间
      that.lastTapTime = currentTime

      // 如果两次点击时间在300毫秒内，则认为是双击事件
      if (currentTime - lastTapTime < 300) {
        console.log("double tap")
        // 成功触发双击事件时，取消单击事件的执行
        clearTimeout(that.lastTapTimeoutFunc);
        var current = e.target.dataset.src;
        var data = t['Poster'];
        var list = [];
        for (var i = 0; i < data.length; i++) {
          list.push(data[i]['Url'])
        }
        console.log('预览图片', list)
        app.previewImage(current, list);
      } else {
        // 单击事件延时300毫秒执行，这和最初的浏览器的点击300ms延时有点像。
        that.lastTapTimeoutFunc = setTimeout(function() {
          console.log("tap")
          app.uploadPoster(that, id);
        }, 300);
      }
    }
  },
  //删除图片
  deleteList: function(e) {
    var that = this;
    var id = e.currentTarget.id; //获取当前长按图片ID
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function(res) {
        if (res.confirm) {
          that.deletePost(id)
        } else if (res.cancel) {
          console.log('点击取消了');
          return false;
        }
      }
    })
  },
  choicePoster: function(e) {
    var that = this;
    var t = that.data;
    var id = e.currentTarget.id;
    var num = t.count * 1 - t.userCardInfo['Poster'].length
    app.uploadPoster(that,num,id);
  },
  goMessage: function() {
    wx.navigateTo({
      url: '../message/index'
    })
  },
  goOpenRedpag: function() {
    wx.switchTab({
      url: '../openRedpag/index',
    })
  },
  goEditCradInfo: function() {
    wx.navigateTo({
      url: '../editCardInfo/index'
    })
  },
  getPersonalUserInfo() {
    var that = this;
    var data = [];
    data['uid'] = uid;
    var url = 'We_Community/getBrowsePersonalUserInfo';
    var calkdata = app.netWorkRequest(that, data, url, 'userCardInfo')
  },
  deletePost: function(id) {
    var that = this;
    var data = [];
    data['id'] = id;
    var url = 'We_Businesscard/getDellUserPost';
    var calkdata = app.netWorkRequest(that, data, url, 'updatePoster')
    console.log('下表', data)
    app.clickLongdeletion(that, data, index, 'redPackList');
  },
  updatePoster: function(data) {
    var that = this;
    var t = that.data.userCardInfo;
    t['Poster'] = data;
    that.setData({
      userCardInfo: t
    })
  },
  /**
   * 弹窗
   */
  showDialogBtn: function() {
    this.setData({
      showModal: true
    })
  },

  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function() {},
  /**
   * 隐藏模态对话框
   */
  hideModal: function() {
    this.setData({
      showModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function() {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function() {
    wx.showToast({
      title: '提交成功',
      icon: 'success',
      duration: 2000
    })
    this.hideModal();
  },
  //点击按钮痰喘指定的hiddenmodalput弹出框  
  messageModalput: function() {
    this.setData({
      messageModalput: !this.data.messageModalput
    })
  },
  messageComplete: function(e) {
    var msg = e.detail.value;
    var that = this;
    var t = that.data;
    var data = t.userCardInfo;
    data['Introduce'] = msg;
    that.setData({
      data: data
    })
  },
  //取消按钮  
  cancel: function() {
    var that = this;
    this.setData({
      messageModalput: true,
    });
  },
  //确认  
  confirm: function(e) {
    var that = this;
    var t = that.data;
    var msg = t.userCardInfo['Introduce'];
    var data = [];
    data['field'] = 'Introduce';
    data['Introduce'] = msg;
    var url = 'We_Businesscard/getUpdateUserCardInfo';
    var calkdata = app.netWorkRequest(that, data, url, 'Introduce')
    that.setData({
      messageModalput: true,
      message: ''
    })
  },
  updateIntroduce: function(msg) {
    console.log('缔造', msg)
    var that = this;
    var data = that.data.userCardInfo;
    //data['Introduce'] = that.data.message;
    data['Introduce'] = msg;
    data['IntState'] = app.exceptLengthLine(msg);
    that.setData({
      userCardInfo: data
    })
  },
  //初始化页面数据
  initData: function() {
    var that = this;
    that.setData({
      canse: false,
      messageModalput: true,
      message: '',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('userInfo获取参数', options)
    wx.hideShareMenu();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.baseImage();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log('UserInfo页面显示')
    this.setData({
      canse: false
    })
    this.getStorage();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    var that = this;
    that.setData({
      hitenTop: true
    })
    var t = that.data;
    var ufo = t.userCardInfo;
    var mid = ufo['User_id']; //被分享者ID
    var title = '';
    var path = 'pages/visitorInfo/index?id=' + mid;
    var imageUrl = '';
    console.log('分享信息' + path)
    if (res.from === 'button') {
      title = ufo['Username'] + '给你发来了一个名片红包';
      setTimeout(function () {
        that.setData({
          hitenTop: false
        })
      }, 3000)
    } else {
      title = '联首名片给你发来了一个红包';
      imageUrl = app.globalData.shareImg;
    }
    return {
      title: title,
      path: path,
      imageUrl: imageUrl,
    }
  }
})